﻿namespace IntellectShop.Web2.Hardware.Code
{
	using System.Globalization;
	using System.Web.Compilation;
	using System.Web.Mvc;

	public static class LocalizationExtensions
	{
		public static string Resource(this HtmlHelper html, string expression, params object[] args)
		{
			string path = ((WebFormView)html.ViewContext.View).ViewPath;

			ResourceExpressionFields fields = (ResourceExpressionFields)(new ResourceExpressionBuilder()).ParseExpression(
				expression,
				typeof(string),
				new ExpressionBuilderContext(path)
				);

			return (!string.IsNullOrEmpty(fields.ClassKey))
				? string.Format((string)html.ViewContext.HttpContext.GetGlobalResourceObject(
					fields.ClassKey,
					fields.ResourceKey,
					CultureInfo.CurrentUICulture),
					args)

				: string.Format((string)html.ViewContext.HttpContext.GetLocalResourceObject(
					path,
					fields.ResourceKey,
					CultureInfo.CurrentUICulture),
					args);
		}
	}
}
